from time import sleep
from base64 import b64decode
import requests
import os
import sys
from PyQt5.QtCore import QThread,pyqtSignal,QDateTime,Qt
from PyQt5.QtGui import QIcon,QPixmap,QImage,QCursor
from PyQt5.QtWidgets import QMainWindow,QMessageBox,QLabel,QWidget,QComboBox,QGridLayout,QListWidget,QPushButton,QLineEdit,QDesktopWidget,QApplication,QTextEdit
#系统配置
from ctypes import windll
windll.shell32.SetCurrentProcessExplicitAppUserModelID("myappid")


# 初始资源对象类（未定义）
class Dim(object):

    pass


# 多线程时间流
class BackendThread(QThread):
    update_date = pyqtSignal(str)

    def run(self):
        while True:
            data = QDateTime.currentDateTime()
            currtime = data.toString("yyyy-MM-dd hh:mm:ss")
            self.update_date.emit(str(currtime))
            sleep(1)



class MenameUi(QMainWindow):
    def __init__(self, parent=None):
        super(MenameUi, self).__init__(parent)
        
        # 窗口重要属性
        self.setWindowOpacity(0.95) # 设置窗口背景透明度
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)  # Qt主界面底面免显
        self.setWindowTitle("随机点名器")
        
        # icon base 64 转换码
        self.icobase = ""

        self.qrPixmap = QPixmap(QImage.fromData(b64decode(self.icobase)))
        self.setWindowIcon(QIcon(self.qrPixmap))

        
        # 涵数与定义
        self.m_drag = False

        # 主线加载
        self.main_ui()
        self.winset_center(4,3) #窗口宽度为屏幕1/4，高度为屏幕1/3 
        self.design()
        self.area()
        self.qss_jax()


    # 窗口底层面板
    def main_ui(self):

        self.main_widget = QWidget()  # 创建窗口主部件
        self.main_layout = QGridLayout()  # 创建主部件的网格布局
        self.main_widget.setLayout(self.main_layout)  # 设置窗口主部件布局为网格布局

        self.left_widget = QWidget()  # 创建左侧部件
        self.left_widget.setObjectName('left_widget')
        self.left_layout = QGridLayout()  # 创建左侧部件的网格布局层
        self.left_widget.setLayout(self.left_layout)  # 设置左侧部件布局为网格

        self.right_widget = QWidget()  # 创建右侧部件
        self.right_widget.setObjectName('right_widget')
        self.right_layout = QGridLayout()
        self.right_widget.setLayout(self.right_layout)  # 设置右侧部件布局为网格

        self.main_layout.addWidget(self.left_widget, 0, 0, 10, 3)  # 左侧部件在第0行第0列
        self.main_layout.addWidget(self.right_widget, 0, 4, 10, 9)  # 右侧部件在第0行第4列
        self.setCentralWidget(self.main_widget)  # 设置窗口主部件


    # 控件设计
    def design(self):

        # 左侧控件
        self.bx = QPushButton(self)
        self.bx.setObjectName('bx')
        self.bx.clicked.connect(self.close)

        self.bm = QPushButton(self)
        self.bm.setObjectName('bm')

        self.bp = QPushButton(self)
        self.bp.setObjectName('bp')

        # 设置三按钮格律化大小
        self.bx.setFixedSize(15,15)
        self.bm.setFixedSize(15,15)
        self.bp.setFixedSize(15,15)
      

        self.logo = QLabel(self)
        self.logo.setObjectName('logo')
        self.logo.setText("Mename")

        self.emo = QLabel(self)
        self.emo.setObjectName('emo')
        self.emo.setText("#v#")

        self.bfile = QPushButton(self)
        self.bfile.setObjectName('bfile')

        # 空白格，位置填充
        self.left_empty = QLabel(self)
        self.left_empty.setObjectName("empty")

        # 右侧控件
        self.ghe = QLabel(self)    # 标识
        self.ghe.setStyleSheet("background-color:rgb(180,180,180)") # 便于前期识别，非最终结果

        self.groupen = QLabel(self)  # 目前组别显示
        self.groupen.setStyleSheet("background-color:rgb(180,180,180)") # 便于前期识别，非最终结果

        self.grs = QPushButton(self)  # 组别修改

        self.PSC = QLabel(self)  # 姓名显示
        self.PSC.setText("<p align='center'>姓名显示</p>")
        self.PSC.setStyleSheet("color:white;font-size:25pt;background-color:rgb(180,180,180)") # 便于前期识别，非最终结果

        self.b1 = QPushButton(self)  # 开始按钮
        self.b1.setText("开始")

        self.b2 = QPushButton(self)  # 结束按钮
        self.b2.setText("结束")

        self.b3 = QPushButton(self)  # SAY 讲述人
        self.b3.setText("SAY")

        self.b4 = QPushButton(self)  # 倒计时开启按钮
        self.b4.setText("倒计时")

    # 布局
    def area(self):

        # 左布局

        self.left_layout.addWidget(self.bx,1,1,1,1)
        self.left_layout.addWidget(self.bm,1,2,1,1)
        self.left_layout.addWidget(self.bp,1,3,1,1)
        self.left_layout.addWidget(self.logo,2,1,1,3)
        self.left_layout.addWidget(self.emo,3,1,2,3)
        self.left_layout.addWidget(self.bfile,5,1,1,3)
        self.left_layout.addWidget(self.left_empty,6,1,3,3)

        # 右布局

        self.right_layout.addWidget(self.ghe,1,1,1,2)
        self.right_layout.addWidget(self.groupen,1,3,1,4)
        self.right_layout.addWidget(self.grs,1,7,1,2)
        self.right_layout.addWidget(self.PSC,2,1,5,8)
        self.right_layout.addWidget(self.b1,7,1,1,2)
        self.right_layout.addWidget(self.b2,7,3,1,2)
        self.right_layout.addWidget(self.b3,7,5,1,2)
        self.right_layout.addWidget(self.b4,7,7,1,2)




    # 居中显示与窗口调整#
    def winset_center(self,wint,hint):
        screen = QDesktopWidget().screenGeometry()

        self.ww = int(screen.width() / wint)
        self.wh = int(screen.height() / hint)
        self.setFixedSize(self.ww, self.wh)

        size = self.geometry()
        self.wx = (screen.width() - size.width()) / 2
        self.wy = (screen.height() - size.height()) / 2

        # self.setSize(self.winwidth, self.winheight)

        self.move(int(self.wx), int(self.wy))
     

    
    #QSS界面
    def qss_jax(self):

        qsstyle = '''
            
            QWidget#left_widget{
                background:rgb(90,90,90);
                border-top:1px solid gray;
                border-bottom:1px solid gray;
                border-left:1px solid gray;
                border-right:1px solid gray;
                border-top-left-radius:10px;
                border-bottom-left-radius:10px;
                border-top-right-radius:10px;
                border-bottom-right-radius:10px;}
            
            
            QWidget#right_widget{
                color:rgb(200,000,200);
                background:rgb(235,235,235);
                border-top:1px solid darkGray;
                border-bottom:1px solid darkGray;
                border-left:1px solid darkGray;
                border-right:1px solid darkGray;
                border-top-left-radius:10px;
                border-bottom-left-radius:10px;
                border-top-right-radius:10px;
                border-bottom-right-radius:10px;}
            
            
        
            QPushButton#bx{background:#F76677;border-radius:5px;}
            QPushButton#bm{background:#F7D674;border-radius:5px;}
            QPushButton#bp{background:'sky blue';border-radius:5px;}
            
            QPushButton#bx:hover{background:red}
            QPushButton#bm:hover{background:yellow}
            QPushButton#bp:hover{background:blue}
        
        
        
        '''

        self.setStyleSheet(qsstyle)

    
    # 鼠标移动事件
    def mouseMoveEvent(self, event):

        if Qt.LeftButton and self.m_drag:
            self.move(event.globalPos() - self.m_dragposition)
            event.accept()

        self.update()

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.m_drag = True
            self.m_dragposition = event.globalPos() - self.pos()
            event.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))

    def mouseReleaseEvent(self, event):

        # 重写鼠标按住后松开的事件

        # 窗口移动
        self.m_drag = False
        self.setCursor(QCursor(Qt.ArrowCursor))

        self.update()



if __name__ == "__main__":
    app = QApplication(sys.argv)

    win = MenameUi()
    
    win.show()

    sys.exit(app.exec_())
